package org.lboot.flow.parser;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.lboot.flow.constant.FlowNodeTypeConst;
import org.lboot.flow.model.FlowModel;
import org.lboot.flow.model.node.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 节点解析器
 * @author MLD
 * jackson 解析 -> hutool 方法替代
 */
@Deprecated
@Service
@Slf4j
public class FlowNodeParser {
    private ObjectMapper objectMapper = new ObjectMapper();
    private final static String FLOW_ID="flowId";
    private final static String FLOW_NAME="flowName";
    private final static String NODE_LIST="nodeList";
    private final static String NODE_TYPE="nodeType";

    /**
     * 解析流程定义
     * @param flowDefine
     * @return
     * @throws IOException
     */
//    public FlowModel parser(String flowDefine) throws IOException {
//        JsonNode root = objectMapper.readTree(flowDefine);
//        if(!root.has(FLOW_ID)||!root.has(FLOW_NAME)||!root.has(NODE_LIST)) {
//            return null;
//        }
//        String flowId = root.get(FLOW_ID).asText();
//        String flowName = root.get(FLOW_NAME).asText();
//        JsonNode nodeList = root.get("nodeList");
//        if(!nodeList.isArray()) {
//            return null;
//        }
//        FlowModel flowModel = new FlowModel();
//        flowModel.setFlowCode(flowId);
//        flowModel.setFlowName(flowName);
//        flowModel.setNodeList(new ArrayList<BaseModel>());
//        Iterator<JsonNode> nodes = nodeList.elements();
//        while (nodes.hasNext()) {
//            JsonNode node = nodes.next();
//            if(node.has(NODE_TYPE)) {
//                String nodeType = node.get(NODE_TYPE).asText();
//                if(FlowNodeTypeConst.START_NODE.equals(nodeType)) {
//                    // 开始节点
//                    StartModel model = JSONUtil.toBean(node.toString(),StartModel.class);
//                    flowModel.getNodeList().add(model);
//                } else if(FlowNodeTypeConst.TASK_NODE.equals(nodeType)) {
//                    // 任务节点
//                    TaskModel model = JSONUtil.toBean(node.toString(),TaskModel.class);
//                    flowModel.getNodeList().add(model);
//                } else if(FlowNodeTypeConst.END_NODE.equals(nodeType)) {
//                    // 结束节点
//                    EndModel model = JSONUtil.toBean(node.toString(),EndModel.class);
//                    flowModel.getNodeList().add(model);
//                } else if(FlowNodeTypeConst.GATEWAY_NODE.equals(nodeType)){
//                    // 网关节点
////                    GateWayModel model = objectMapper.readValue(node.toString(), GateWayModel.class);
////                    flowModel.getNodeList().add(model);
//                } else if(FlowNodeTypeConst.TRIGGER_NODE.equals(nodeType)){
//                    // 触发器节点
//                    TriggerModel model = JSONUtil.toBean(node.toString(),TriggerModel.class);
//                    flowModel.getNodeList().add(model);
//                }else if(FlowNodeTypeConst.DECISION_TASK_NODE.equals(nodeType)){
//                    //DecisionTaskModel model = objectMapper.readValue(node.toString(), DecisionTaskModel.class);
//                    DecisionTaskModel model = JSONUtil.toBean(node.toString(),DecisionTaskModel.class);
//                    flowModel.getNodeList().add(model);
//                }else if (FlowNodeTypeConst.SUB_FLOW_NODE.equals(nodeType)){
//                    SubFlowModel model = JSONUtil.toBean(node.toString(),SubFlowModel.class);
//                    flowModel.getNodeList().add(model);
//                }else if (FlowNodeTypeConst.PARALLEL_GATEWAY_NODE.equals(nodeType)){
//                    ParallelGatewayModel model = JSONUtil.toBean(node.toString(),ParallelGatewayModel.class);
//                    flowModel.getNodeList().add(model);
//                }else if (FlowNodeTypeConst.EXCLUSIVE_GATEWAY_NODE.equals(nodeType)){
//                    ExclusiveGatewayModel model = JSONUtil.toBean(node.toString(),ExclusiveGatewayModel.class);
//                    flowModel.getNodeList().add(model);
//                }else if (FlowNodeTypeConst.EVENT_NODE.equals(nodeType)){
//                    EventModel model = JSONUtil.toBean(node.toString(),EventModel.class);
//                    flowModel.getNodeList().add(model);
//                }
//            }
//        }
//        return flowModel;
//    }
}

